package com.canvas.service.house;

import com.canvas.dto.*;
import com.canvas.pojo.Subway;
import com.canvas.pojo.SubwayStation;
import com.canvas.pojo.SupportAddress;
import com.canvas.repository.SubwayRepository;
import com.canvas.repository.SubwayStationRepository;
import com.canvas.repository.SupportAddressRespository;
import com.google.common.base.Strings;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 宗恣
 * @Date: 2018/9/4 0004 10:08
 */
@Service
public class IAddressServiceImpl implements IAddressService {

    @Autowired
    private SupportAddressRespository supportAddressRespository;

    @Autowired
    private SubwayRepository subwayRepository;

    @Autowired
    private SubwayStationRepository subwayStationRepository;

    @Autowired
    private ModelMapper modelMapper;

    /**
     * 获取所有支持的城市列表
     * @return
     */
    @Override
    public ServiceMultiResult<SupportAddressDTO> findAllCities() {

        List<SupportAddress> addresses = supportAddressRespository.findAllByLevel(SupportAddress.Level.CITY.getValue());

        List<SupportAddressDTO> addressDTOS = addresses.stream()
                .map(supportAddress -> modelMapper.map(supportAddress, SupportAddressDTO.class))
                .collect(Collectors.toList());

        return new ServiceMultiResult<>(addressDTOS.size(), addressDTOS);
    }

    /**
     * 根据城市英文简写获取该城市所有支持的区域信息
     * @param cityName
     * @return
     */
    @Override
    public ServiceMultiResult<SupportAddressDTO> findAllRegionsByCityName(String cityName) {

        List<SupportAddress> regions = supportAddressRespository.findAllByLevelAndBelongTo(SupportAddress.Level.REGION.getValue(), cityName);

        if (regions.isEmpty() || regions.size() <= 0) {
            return new ServiceMultiResult<>(0, null);
        }

        List<SupportAddressDTO> result = regions.stream()
                .map(supportAddress -> modelMapper.map(supportAddress, SupportAddressDTO.class))
                .collect(Collectors.toList());

        return new ServiceMultiResult<>(result.size(), result);
    }

    /**
     * 获取该城市所有的地铁线路
     * @param cityEnName
     * @return
     */
    @Override
    public ServiceMultiResult<SubwayDTO> findAllSubwayByCity(String cityEnName) {

        List<Subway> subways = subwayRepository.findAllByCityEnName(cityEnName);

        if (subways.isEmpty() || subways.size() <= 0) {
            return new ServiceMultiResult<>(0, null);
        }

        List<SubwayDTO> result = subways.stream()
                .map(subway -> modelMapper.map(subway, SubwayDTO.class))
                .collect(Collectors.toList());

        return new ServiceMultiResult<>(result.size(), result);
    }

    /**
     * 获取地铁线路所有的站点
     * @param subwayId
     * @return
     */
    @Override
    public ServiceMultiResult<SubwayStationDTO> findAllStationBySubway(Long subwayId) {

        List<SubwayStation> stations = subwayStationRepository.findAllBySubwayId(subwayId);

        if (stations.isEmpty() || stations.size() <= 0) {
            return new ServiceMultiResult<>(0, null);
        }

        List<SubwayStationDTO> result = stations.stream()
                .map(subwayStation -> modelMapper.map(subwayStation, SubwayStationDTO.class))
                .collect(Collectors.toList());

        return new ServiceMultiResult<>(result.size(), result);
    }

    @Override
    public Map<SupportAddress.Level, SupportAddressDTO> findCityAndRegion(String cityEnName, String regionEnName) {
        Map<SupportAddress.Level, SupportAddressDTO> result = new HashMap<>();

        SupportAddress city = supportAddressRespository.findByEnNameAndLevel(cityEnName, SupportAddress.Level.CITY
                .getValue());
        SupportAddress region = supportAddressRespository.findByEnNameAndBelongTo(regionEnName, city.getEnName());

        result.put(SupportAddress.Level.CITY, modelMapper.map(city, SupportAddressDTO.class));
        result.put(SupportAddress.Level.REGION, modelMapper.map(region, SupportAddressDTO.class));
        return result;
    }

    @Override
    public ServiceResult<SubwayDTO> findSubway(Long subwayId) {

        if (null == subwayId) {
            return ServiceResult.notFound();
        }

        Subway subway = subwayRepository.findById(subwayId).get();

        if (null == subway) {
            return ServiceResult.notFound();
        }

        return ServiceResult.of(modelMapper.map(subway, SubwayDTO.class));
    }

    @Override
    public ServiceResult<SubwayStationDTO> findSubwayStation(Long stationId) {

        if (stationId == null) {
            return ServiceResult.notFound();
        }
        SubwayStation station = subwayStationRepository.findById(stationId).get();
        if (station == null) {
            return ServiceResult.notFound();
        }
        return ServiceResult.of(modelMapper.map(station, SubwayStationDTO.class));
    }

    @Override
    public ServiceResult<SupportAddressDTO> findCity(String cityEnName) {

        if (Strings.isNullOrEmpty(cityEnName)) {
            return ServiceResult.notFound();
        }

        SupportAddress supportAddress = supportAddressRespository.findByEnNameAndLevel(cityEnName, SupportAddress.Level.CITY.getValue());
        if (null == supportAddress) {
            return ServiceResult.notFound();
        }

        SupportAddressDTO addressDTO = modelMapper.map(supportAddress, SupportAddressDTO.class);

        return ServiceResult.of(addressDTO);
    }
}
